<?php
include_once XXF_EXCEL;
class RunExcel{
	private $excel;
	private $sheet;
	private $header;
	private $letter=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AZ','AY','AZ','BA','BB');
	function __construct(){
    }
	public function createExcel(){
		$this->excel=new PHPExcel();
		$this->sheet=$this->excel->getSheet(0);
		$this->sheet->getDefaultRowDimension()->setRowHeight(15);
	}
	public function fromExcel($url){
	    $e5=new PHPExcel_Reader_Excel5();
	    $this->excel=$e5->load($url);
	    $this->sheet=$this->excel->getSheet(0);
	}
	public function getSheet(){
	    return $this->sheet;
	}
	public function activeSheet($a=0){
		$this->sheet=$this->excel->getSheet($a);
	}
	public function setActiveSheetTitle($str){
        $this->sheet->setTitle($str);
	}
	public function createSheet(){
	    $this->excel->createSheet();
	}
	public function setHeader($array){
	    $letter=$this->letter;
	    for($i = 0;$i < count($array);$i++) {
	        $this->sheet->setCellValue("$letter[$i]1","$array[$i]");
	        $this->sheet->getStyle("$letter[$i]1")->applyFromArray(array(
	            'font'=>array('bold'=>true)
	        ));
	    }
	}
	public function setBody($array){
	    $letter=$this->letter;
	    $j=2;
	    $i=0;
	    foreach ($array as $row){
	        for($i = 0;$i < count($row);$i++) {
	            $this->sheet->setCellValue("$letter[$i]".$j,"$row[$i]");
	        }
	        $j++;
	    }
	    $this->sheet->getStyle("A1:".$letter[($i-1)].($j-1))->applyFromArray(array(
	        'borders'=>array('allborders'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN))
	    ));
	}
	public function getSheetArray(){
	    return $this->sheet->toArray();
	}
	public function down($fileTitle="excel"){
	    header("Content-Type:text/html;charset=utf-8");
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
	    $write = new PHPExcel_Writer_Excel5($this->excel);
	    header("Pragma: public");
	    header("Expires: 0");
	    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	    header("Content-Type:application/force-download");
	    header("Content-Type:application/vnd.ms-execl");
	    header("Content-Type:application/octet-stream");
	    header("Content-Type:application/download");;
	    header('Content-Disposition:attachment;filename="'.$fileTitle.'.xls"');
	    header("Content-Transfer-Encoding:binary");
	    $write->save('php://output');
	}
}